package com.example.myapplication.ui.components

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.clickable
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import com.example.myapplication.data.model.StorageItem
import com.example.myapplication.data.model.StorageSpace

@Composable
fun MoveItemDialog(
    itemToMove: StorageItem,
    allSpaces: List<StorageSpace>,
    onDismiss: () -> Unit,
    onConfirm: (newSpaceId: String) -> Unit
) {
    var selectedSpaceId by remember { mutableStateOf<String?>(null) }

    Dialog(onDismissRequest = onDismiss) {
        Card(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp)
        ) {
            Column(
                modifier = Modifier.padding(24.dp)
            ) {
                Text(
                    text = "将 '${itemToMove.name}' 移动到...",
                    style = MaterialTheme.typography.headlineSmall
                )
                Spacer(modifier = Modifier.height(16.dp))
                LazyColumn(
                    modifier = Modifier.heightIn(max = 300.dp)
                ) {
                    items(allSpaces) { space ->
                        Row(
                            modifier = Modifier
                                .fillMaxWidth()
                                .clickable { selectedSpaceId = space.id }
                                .padding(vertical = 8.dp)
                        ) {
                            RadioButton(
                                selected = selectedSpaceId == space.id,
                                onClick = { selectedSpaceId = space.id }
                            )
                            Spacer(modifier = Modifier.width(8.dp))
                            Text(text = space.name)
                        }
                    }
                }
                Spacer(modifier = Modifier.height(16.dp))
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.End
                ) {
                    TextButton(onClick = onDismiss) {
                        Text("取消")
                    }
                    Spacer(modifier = Modifier.width(8.dp))
                    Button(
                        onClick = {
                            selectedSpaceId?.let { onConfirm(it) }
                        },
                        enabled = selectedSpaceId != null
                    ) {
                        Text("移动")
                    }
                }
            }
        }
    }
}